package com.echo.neocommunity.service.impl;

import com.echo.neocommunity.utils.RedisKeyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class FollowService {
    @Autowired
    private RedisTemplate redisTemplate;


    public void followee(int userId,int entityType,int entityId){
        redisTemplate.execute(new SessionCallback() {
            @Override
            public Object execute(RedisOperations operations) throws DataAccessException {
                String followeeKey = RedisKeyUtils.getFolloweeKey(userId, entityType);
                String followerKey = RedisKeyUtils.getFollowerKey(entityType, entityId);
                operations.multi();
                operations.opsForZSet().add(followeeKey,entityId,System.currentTimeMillis());
                operations.opsForZSet().add(followerKey,userId,System.currentTimeMillis());
                return operations.exec();
            }
        });
    }

    public void cancelFollowee(int userId,int entityType,int entityId){
        redisTemplate.execute(new SessionCallback() {
            @Override
            public Object execute(RedisOperations operations) throws DataAccessException {
                String followeeKey = RedisKeyUtils.getFolloweeKey(userId, entityType);
                String followerKey = RedisKeyUtils.getFollowerKey(entityType, entityId);
                operations.multi();
                operations.opsForZSet().remove(followeeKey,entityId);
                operations.opsForZSet().remove(followerKey,userId);
                return operations.exec();
            }
        });
    }

    public long findFolloweeCount(int userId,int entityType){
        String followeeKey = RedisKeyUtils.getFolloweeKey(userId, entityType);
        return redisTemplate.opsForZSet().zCard(followeeKey);
    }

    public long findFollowerCount(int entityType,int entityId){
        String followerKey = RedisKeyUtils.getFollowerKey(entityType, entityId);
        return redisTemplate.opsForZSet().zCard(followerKey);
    }

    public boolean findFolloweeStatus(int userId,int entityType,int entityId){
        String followeeKey = RedisKeyUtils.getFolloweeKey(userId, entityType);
        return redisTemplate.opsForZSet().score(followeeKey,entityId)!=null;
    }

    public boolean findFollowerStatus(int userId,int entityType,int entityId){
        String followerKey = RedisKeyUtils.getFollowerKey( entityType,userId);
        return redisTemplate.opsForZSet().score(followerKey,entityId)!=null;
    }

    public Set<Integer> getFollowees(int userId,int entityType,int offset,int limit){
        String followeeKey = RedisKeyUtils.getFolloweeKey(userId, entityType);
        return redisTemplate.opsForZSet().range(followeeKey, offset, limit);
    }

    public Date getFolloweesDate(int userId,int entityType,int followeeUserId){
        String followeeKey = RedisKeyUtils.getFolloweeKey(userId, entityType);
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Double score = redisTemplate.opsForZSet().score(followeeKey, followeeUserId);
        String format1 = format.format(score);
        Date parse = null;
        try {
            parse = format.parse(format1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return  parse;
    }

    public Set<Integer> getFollowers(int entityType,int entityId,int offset,int limit){
        String followerKey = RedisKeyUtils.getFollowerKey(entityType, entityId);
        return  redisTemplate.opsForZSet().range(followerKey,offset,limit);
    }

    public Date getFollowersDate(int userId,int entityType,int followerUserId){
        String followerKey = RedisKeyUtils.getFollowerKey(entityType, userId);
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Double score = redisTemplate.opsForZSet().score(followerKey, followerUserId);
        String format1 = format.format(score);
        Date parse = null;
        try {
            parse = format.parse(format1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return  parse;
    }




}
